<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="https://gitee.com/reku1997/reku1997/raw/master/reku.ico">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"reku1997.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"appID":"AW5K8S9IEE","apiKey":"d7e666d597854738d2fb31ecaa989aa5","indexName":"dev_reku1997","hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="A,B略">
<meta property="og:type" content="article">
<meta property="og:title" content="Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)">
<meta property="og:url" content="https://reku1997.gitee.io/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/index.html">
<meta property="og:site_name" content="Reku">
<meta property="og:description" content="A,B略">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-05-16T09:11:12.000Z">
<meta property="article:modified_time" content="2021-12-16T11:32:41.000Z">
<meta property="article:author" content="Reku">
<meta property="article:tag" content="codeforces">
<meta property="article:tag" content="树状数组">
<meta property="article:tag" content="折半搜索">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://reku1997.gitee.io/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-cn'
  };
</script>

  <title>Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) | Reku</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="Reku" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Reku</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>Sitemap</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-cn">
    <link itemprop="mainEntityOfPage" href="https://reku1997.gitee.io/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Reku">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reku">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2017-05-16 17:11:12" itemprop="dateCreated datePublished" datetime="2017-05-16T17:11:12+08:00">2017-05-16</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-12-16 19:32:41" itemprop="dateModified" datetime="2021-12-16T19:32:41+08:00">2021-12-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/acm/" itemprop="url" rel="index"><span itemprop="name">acm</span></a>
                </span>
            </span>

          
            <span id="/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/" class="post-meta-item leancloud_visitors" data-flag-title="Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)" title="Views">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2017/05/16/playrix-codescapes-cup-codeforces-round-413-rated-div-1-div-2/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>A,B略 <span id="more"></span></p>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/799/problem/C">C. Fountains</a> 一开始看错了题，还以为是什么很牛逼的背包题。 认真读了读题目，发现只需要两个喷泉，所以要么两个C、两个D、一个C一个D，用树状数组乱搞搞就行，很简单。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BITree</span>&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">long</span> <span class="keyword">long</span> SIZE = <span class="number">100010</span>, BIAS = <span class="number">5</span>;</span><br><span class="line">    <span class="keyword">long</span> <span class="keyword">long</span> u[SIZE];</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="built_in">memset</span>(u,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(u));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> v)</span></span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(x+=BIAS;x&lt;SIZE;x+=x&amp;-x) u[x]=<span class="built_in">max</span>(v,u[x]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">getmax</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x)</span></span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> s=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(x+=BIAS;x;x-=x&amp;-x) s=<span class="built_in">max</span>(s,u[x]);</span><br><span class="line">        <span class="keyword">return</span> s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125; cc,dd;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	cc.<span class="built_in">clear</span>();</span><br><span class="line">	dd.<span class="built_in">clear</span>();</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> mac=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> mad=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> n,c,d;</span><br><span class="line">	cin&gt;&gt;n&gt;&gt;c&gt;&gt;d;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">long</span> <span class="keyword">long</span> x,y;</span><br><span class="line">		<span class="keyword">char</span> ch;</span><br><span class="line">		cin&gt;&gt;x&gt;&gt;y&gt;&gt;ch;</span><br><span class="line">		<span class="keyword">if</span>(ch==<span class="string">&#x27;C&#x27;</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(y&lt;=c)&#123; mac=<span class="built_in">max</span>(mac,x);</span><br><span class="line">			<span class="keyword">long</span> <span class="keyword">long</span> p=cc.<span class="built_in">getmax</span>(c-y);</span><br><span class="line">			<span class="keyword">if</span>(p&amp;&amp;x) ans=<span class="built_in">max</span>(ans,p+x);</span><br><span class="line">			cc.<span class="built_in">modify</span>(y,x);&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(y&lt;=d) &#123;mad=<span class="built_in">max</span>(mad,x);</span><br><span class="line">			<span class="keyword">long</span> <span class="keyword">long</span> p=dd.<span class="built_in">getmax</span>(d-y);</span><br><span class="line">			<span class="keyword">if</span>(p&amp;&amp;x) ans=<span class="built_in">max</span>(ans,p+x);</span><br><span class="line">			dd.<span class="built_in">modify</span>(y,x);&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(mac&amp;&amp;mad) ans=<span class="built_in">max</span>(ans,mac+mad);</span><br><span class="line">	cout&lt;&lt;ans&lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/799/problem/D">D. Field expansion</a> 题目可以转化为，给一列数字，找不相交两组数字相乘的结果分别大于A和B，使得用到的数字最少。 很容易发现，能用大的数字一定用大的。 因为A和B都不超过<span class="math inline">\(100000\)</span>，所以用不超过<span class="math inline">\(34\)</span>个数字一定可以找到一组合法解，那么显然就用最大的<span class="math inline">\(34\)</span>个数字。 然后判断有没有合法解，就拆成两半，然后把所有解生成出来，一半枚举，另一半二分就可以了，蛮常见的处理方式。 要注意这个长方形是可以旋转的...因为没发现可以旋转，错了好多次QAQ</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> a,b,h,w,n;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> e[<span class="number">100010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> x;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> y;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> x,<span class="keyword">long</span> <span class="keyword">long</span> y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> x&gt;y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">pan</span><span class="params">(<span class="keyword">int</span> t)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> num2=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> p[<span class="number">2</span>][<span class="number">30</span>];</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> ma=<span class="number">1ll</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> u=(<span class="number">2ll</span>&lt;&lt;<span class="number">40</span>);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=t/<span class="number">2</span>;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		num1++;</span><br><span class="line">		p[<span class="number">0</span>][num1]=e[i];</span><br><span class="line">		<span class="keyword">if</span>(ma&lt;u) ma*=e[i];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=t-num1;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		num2++;</span><br><span class="line">		p[<span class="number">1</span>][num2]=e[i+num1];</span><br><span class="line">		<span class="keyword">if</span>(ma&lt;u) ma*=e[i+num1];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(ma&lt;x*y) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; z1;</span><br><span class="line">	vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; z2;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=num1;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> g=z1.<span class="built_in">size</span>();</span><br><span class="line">		z1.<span class="built_in">pb</span>(p[<span class="number">0</span>][i]);</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;g;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(z1[j]*p[<span class="number">0</span>][i]&lt;=<span class="number">100000</span>) z1.<span class="built_in">pb</span>(z1[j]*p[<span class="number">0</span>][i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=num2;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> g=z2.<span class="built_in">size</span>();</span><br><span class="line">		z2.<span class="built_in">pb</span>(p[<span class="number">1</span>][i]);</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;g;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(z2[j]*p[<span class="number">1</span>][i]&lt;=<span class="number">100000</span>) z2.<span class="built_in">pb</span>(z2[j]*p[<span class="number">1</span>][i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	z1.<span class="built_in">pb</span>(<span class="number">1</span>);</span><br><span class="line">	z2.<span class="built_in">pb</span>(<span class="number">1</span>);</span><br><span class="line">	<span class="built_in">sort</span>(z1.<span class="built_in">begin</span>(),z1.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">sort</span>(z2.<span class="built_in">begin</span>(),z2.<span class="built_in">end</span>());</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> pma=<span class="number">222147483600ll</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;z1.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> l=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> r=z2.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">		<span class="keyword">while</span>(l&lt;r)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">			<span class="keyword">if</span>(z1[i]*z2[mid]&gt;=x) r=mid;</span><br><span class="line">			<span class="keyword">else</span> l=mid+<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(z1[i]*z2[l]&gt;=x) pma=<span class="built_in">min</span>(pma,z1[i]*z2[l]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>((ma/pma)&gt;=y) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">int</span> ans=<span class="number">-1</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">suan</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	x=(a/h)+!!(a%h);</span><br><span class="line">	y=(b/w)+!!(b%w);</span><br><span class="line">	<span class="comment">//cout&lt;&lt;x&lt;&lt;&quot; &quot;&lt;&lt;y&lt;&lt;endl;</span></span><br><span class="line">	<span class="keyword">if</span>(x==<span class="number">1</span>&amp;&amp;y==<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		cout&lt;&lt;<span class="number">0</span>&lt;&lt;endl;</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">sort</span>(e+<span class="number">1</span>,e+n+<span class="number">1</span>,cmp);</span><br><span class="line">	<span class="keyword">int</span> l=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">int</span> r=<span class="built_in">min</span>(<span class="number">34ll</span>,n);</span><br><span class="line">	<span class="keyword">while</span>(l&lt;r)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">pan</span>(mid)) r=mid;</span><br><span class="line">		<span class="keyword">else</span> l=mid+<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(<span class="built_in">pan</span>(l)!=<span class="number">0</span>) </span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(ans==<span class="number">-1</span>) ans=l;</span><br><span class="line">		<span class="keyword">else</span> ans=<span class="built_in">min</span>(ans,l);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%lld%lld%lld%lld%lld&quot;</span>,&amp;a,&amp;b,&amp;h,&amp;w,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>,&amp;e[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">suan</span>();</span><br><span class="line">	<span class="built_in">swap</span>(h,w);</span><br><span class="line">	<span class="built_in">suan</span>();</span><br><span class="line">	cout&lt;&lt;ans&lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/799/problem/E">E. Aquarium decoration</a> 把所有的物品分为四份：A和B都喜欢的，A喜欢的，B喜欢的，A和B都不喜欢的。 然后枚举第一部分找多少个，用两个树状数组加速剩下三部分的计算就好了。我的做法是一个树状数组处理前缀个数，然后另一个处理前缀和，到时候二分就可以了。 写错了好多次...码力好差...</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pb push_back</span></span><br><span class="line"><span class="keyword">int</span> n,m,k;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> c[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">bool</span> a[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">bool</span> b[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> qhb[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> qhb1[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> qhb2[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line">vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; bb;</span><br><span class="line">vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; b1;</span><br><span class="line">vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; b2;</span><br><span class="line">vector&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; b3;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">pp</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> s;</span><br><span class="line">	<span class="keyword">int</span> a;</span><br><span class="line">	<span class="keyword">int</span> b;</span><br><span class="line">&#125; e[<span class="number">200010</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(pp x,pp y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> x.s&lt;y.s;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BITree</span>&#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">int</span> SIZE = <span class="number">400010</span>, BIAS = <span class="number">5</span>;</span><br><span class="line">    <span class="keyword">long</span> <span class="keyword">long</span> u[SIZE];</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="built_in">memset</span>(u,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(u));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">long</span> <span class="keyword">long</span> v)</span></span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(x+=BIAS;x&lt;SIZE;x+=x&amp;-x) u[x]+=v;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">getsum</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> s=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(x+=BIAS;x;x-=x&amp;-x) s+=u[x];</span><br><span class="line">        <span class="keyword">return</span> s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125; bh,qh;</span><br><span class="line"><span class="keyword">int</span> ne;</span><br><span class="line"><span class="keyword">int</span> na[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">int</span> nb[<span class="number">200010</span>]=&#123;&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d&quot;</span>,&amp;n,&amp;m,&amp;k);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>,&amp;c[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">int</span> x;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;x);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=x;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> o;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;o);</span><br><span class="line">		a[o]=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;x);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=x;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> o;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;o);</span><br><span class="line">		b[o]=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(a[i]&amp;&amp;b[i]) bb.<span class="built_in">pb</span>(c[i]);</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(a[i]) b1.<span class="built_in">pb</span>(c[i]);</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(b[i]) b2.<span class="built_in">pb</span>(c[i]);</span><br><span class="line">		<span class="keyword">else</span> b3.<span class="built_in">pb</span>(c[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">sort</span>(bb.<span class="built_in">begin</span>(),bb.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">sort</span>(b1.<span class="built_in">begin</span>(),b1.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">sort</span>(b2.<span class="built_in">begin</span>(),b2.<span class="built_in">end</span>());</span><br><span class="line">	<span class="built_in">sort</span>(b3.<span class="built_in">begin</span>(),b3.<span class="built_in">end</span>());</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;bb.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(i==<span class="number">0</span>) qhb[i]=bb[i];</span><br><span class="line">		<span class="keyword">else</span> qhb[i]=qhb[i<span class="number">-1</span>]+bb[i];</span><br><span class="line">	&#125;</span><br><span class="line">	ne=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;b1.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(i==<span class="number">0</span>) qhb1[i]=b1[i];</span><br><span class="line">		<span class="keyword">else</span> qhb1[i]=qhb1[i<span class="number">-1</span>]+b1[i];</span><br><span class="line">		ne++;</span><br><span class="line">		e[ne].s=b1[i];</span><br><span class="line">		e[ne].a=i;</span><br><span class="line">		e[ne].b=<span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;b2.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(i==<span class="number">0</span>) qhb2[i]=b2[i];</span><br><span class="line">		<span class="keyword">else</span> qhb2[i]=qhb2[i<span class="number">-1</span>]+b2[i];</span><br><span class="line">		ne++;</span><br><span class="line">		e[ne].s=b2[i];</span><br><span class="line">		e[ne].a=<span class="number">-1</span>;</span><br><span class="line">		e[ne].b=i;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;b3.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		ne++;</span><br><span class="line">		e[ne].s=b3[i];</span><br><span class="line">		e[ne].a=<span class="number">-1</span>;</span><br><span class="line">		e[ne].b=<span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">sort</span>(e+<span class="number">1</span>,e+ne+<span class="number">1</span>,cmp);</span><br><span class="line">	bh.<span class="built_in">clear</span>(); qh.<span class="built_in">clear</span>();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=ne;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		bh.<span class="built_in">modify</span>(i,<span class="number">1</span>);</span><br><span class="line">		qh.<span class="built_in">modify</span>(i,e[i].s);</span><br><span class="line">		<span class="keyword">if</span>(e[i].a!=<span class="number">-1</span>) na[e[i].a]=i;</span><br><span class="line">		<span class="keyword">if</span>(e[i].b!=<span class="number">-1</span>) nb[e[i].b]=i;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">-1</span>;</span><br><span class="line">	<span class="keyword">bool</span> bj=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=bb.<span class="built_in">size</span>()<span class="number">-1</span>;i&gt;=<span class="number">0</span>;i--)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>((i+<span class="number">1</span>)&gt;m) <span class="keyword">continue</span>;</span><br><span class="line">		<span class="keyword">long</span> <span class="keyword">long</span> mans=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">if</span>((i+<span class="number">1</span>)&gt;=k)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>((m-i<span class="number">-1</span>)&gt;ne) <span class="keyword">break</span>;</span><br><span class="line">			mans+=qhb[i];</span><br><span class="line">			mans+=qh.<span class="built_in">getsum</span>(m-i<span class="number">-1</span>);</span><br><span class="line">			<span class="keyword">if</span>(ans==<span class="number">-1</span>) ans=mans;</span><br><span class="line">			<span class="keyword">else</span> ans=<span class="built_in">min</span>(ans,mans);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> w=k-i<span class="number">-1</span>;</span><br><span class="line">			<span class="keyword">int</span> u=m-i<span class="number">-1</span><span class="number">-2</span>*w;</span><br><span class="line">			<span class="keyword">if</span>(u&lt;<span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">if</span>(w&gt;(<span class="keyword">int</span>)b1.<span class="built_in">size</span>()||w&gt;(<span class="keyword">int</span>)b2.<span class="built_in">size</span>()) <span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">if</span>(i==bb.<span class="built_in">size</span>()<span class="number">-1</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;w<span class="number">-1</span>;j++)</span><br><span class="line">				&#123;</span><br><span class="line">					bh.<span class="built_in">modify</span>(na[j],<span class="number">-1</span>);</span><br><span class="line">					bh.<span class="built_in">modify</span>(nb[j],<span class="number">-1</span>);</span><br><span class="line">					qh.<span class="built_in">modify</span>(na[j],-b1[j]);</span><br><span class="line">					qh.<span class="built_in">modify</span>(nb[j],-b2[j]);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			w--;</span><br><span class="line">			mans+=qhb[i];</span><br><span class="line">			mans+=qhb1[w];</span><br><span class="line">			mans+=qhb2[w];</span><br><span class="line">			bh.<span class="built_in">modify</span>(na[w],<span class="number">-1</span>);</span><br><span class="line">			bh.<span class="built_in">modify</span>(nb[w],<span class="number">-1</span>);</span><br><span class="line">			qh.<span class="built_in">modify</span>(na[w],-b1[w]);</span><br><span class="line">			qh.<span class="built_in">modify</span>(nb[w],-b2[w]);</span><br><span class="line">			<span class="keyword">if</span>(u!=<span class="number">0</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">int</span> l=<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">int</span> r=ne;</span><br><span class="line">				<span class="keyword">while</span>(l&lt;r)</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="keyword">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">					<span class="keyword">if</span>(bh.<span class="built_in">getsum</span>(mid)&gt;=u) r=mid;</span><br><span class="line">					<span class="keyword">else</span> l=mid+<span class="number">1</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span>(bh.<span class="built_in">getsum</span>(l)==u) mans+=qh.<span class="built_in">getsum</span>(l);</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(ans==<span class="number">-1</span>) ans=mans;</span><br><span class="line">			<span class="keyword">else</span> ans=<span class="built_in">min</span>(ans,mans);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(i==<span class="number">0</span>) bj=<span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(bb.<span class="built_in">size</span>()==<span class="number">0</span>) bj=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">if</span>(bj&amp;&amp;<span class="number">2</span>*k&lt;=m&amp;&amp;k&lt;=b1.<span class="built_in">size</span>()&amp;&amp;k&lt;=b2.<span class="built_in">size</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> w=k;</span><br><span class="line">		<span class="keyword">long</span> <span class="keyword">long</span> mans=<span class="number">0</span>;</span><br><span class="line">		w--;</span><br><span class="line">		mans+=qhb1[w];</span><br><span class="line">		mans+=qhb2[w];</span><br><span class="line">		bh.<span class="built_in">modify</span>(na[w],<span class="number">-1</span>);</span><br><span class="line">		bh.<span class="built_in">modify</span>(nb[w],<span class="number">-1</span>);</span><br><span class="line">		qh.<span class="built_in">modify</span>(na[w],-b1[w]);</span><br><span class="line">		qh.<span class="built_in">modify</span>(nb[w],-b2[w]);</span><br><span class="line">		<span class="keyword">int</span> u=m<span class="number">-2</span>*k;</span><br><span class="line">		<span class="keyword">if</span>(u!=<span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">int</span> l=<span class="number">1</span>;</span><br><span class="line">			<span class="keyword">int</span> r=ne;</span><br><span class="line">			<span class="keyword">while</span>(l&lt;r)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">				<span class="keyword">if</span>(bh.<span class="built_in">getsum</span>(mid)&gt;=u) r=mid;</span><br><span class="line">				<span class="keyword">else</span> l=mid+<span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(bh.<span class="built_in">getsum</span>(l)==u) mans+=qh.<span class="built_in">getsum</span>(l);</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				mans=ans;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(ans==<span class="number">-1</span>) ans=mans;</span><br><span class="line">		<span class="keyword">else</span> ans=<span class="built_in">min</span>(ans,mans);</span><br><span class="line">	&#125;</span><br><span class="line">	cout&lt;&lt;ans&lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/codeforces/" rel="tag"># codeforces</a>
              <a href="/tags/%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84/" rel="tag"># 树状数组</a>
              <a href="/tags/%E6%8A%98%E5%8D%8A%E6%90%9C%E7%B4%A2/" rel="tag"># 折半搜索</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/05/14/codeforces-round-412-rated-div-1-based-on-vk-cup-2017-round-3/" rel="prev" title="Codeforces Round #412 (rated, Div. 1, based on VK Cup 2017 Round 3)">
      <i class="fa fa-chevron-left"></i> Codeforces Round #412 (rated, Div. 1, based on VK Cup 2017 Round 3)
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/05/31/codeforces-round-416-div-2/" rel="next" title="Codeforces Round #416 (Div. 2)">
      Codeforces Round #416 (Div. 2) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Reku</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">96</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/wyc-ruiker" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyc-ruiker" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/reku1997" title="ZhiHu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;reku1997" rel="noopener" target="_blank"><i class="fa fa-fw fa-quora"></i>ZhiHu</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://codeforces.com/profile/reku" title="CodeForces → http:&#x2F;&#x2F;codeforces.com&#x2F;profile&#x2F;reku" rel="noopener" target="_blank"><i class="fa fa-fw fa-code"></i>CodeForces</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.linkedin.cn/injobs/in/reku" title="Linkedin → https:&#x2F;&#x2F;www.linkedin.cn&#x2F;injobs&#x2F;in&#x2F;reku" rel="noopener" target="_blank"><i class="fa fa-fw fa-linkedin"></i>Linkedin</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/reku1997" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;reku1997" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/./atom.xml" title="RSS → .&#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Reku</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/js/algolia-search.js"></script>














  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'MWLzM550UOu69h3dgvbbLSsF-gzGzoHsz',
      appKey     : 'gkKnwm9FK0cu3ysJbcggsCDz',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
